/*
	This script file is part of the Sonic 3 A.I.R. script software distribution.
	Copyright (C) 2017-2024 by Eukaryot

	Published under the GNU GPLv3 open source software license, see license.txt
	or https://www.gnu.org/licenses/gpl-3.0.en.html
*/



//-------------------------------------------------------//
// Cutscene: Transition to burning AIZ                   //
//-------------------------------------------------------//

//# address-hook(0x0684ec) end(0x0684fa)
function void AIZ1.Transition.Update()
{
	// Called addresses:
	//  - 0x068508	-> objA0.base_state = 0x00	-> Initialization
	//  - 0x06852c	-> objA0.base_state = 0x02	-> Waiting until character reaches the right position
	//  - 0x068574	-> objA0.base_state = 0x04	-> Short wait time
	//  - 0x0685b8	-> objA0.base_state = 0x06	-> Flying in
	//  - 0x0685fc	-> objA0.base_state = 0x08	-> Firing its rockets
	call tableLookupAddress(0x0684fe, objA0.base_state)

#if STANDALONE
	// Not in Time Attack please, as this can do funny things like spawn a sign post
	if (!Game.isTimeAttack())
#endif
	{
		Boss.AIZ1.RegularUpdate()
	}
}


//# address-hook(0x068508) end(0x068526)
function void fn068508()
{
#if STANDALONE
	// Move to the right for widescreen
	objA0.position.x.u16 += getScreenExtend()
#endif

	setupObjectAttributesFull(0x068ffe)

	u8[A0 + 0x29] = 0x60
	screenmover_target.right = move_area.right
	level.boss_encounter = 1

	requestLoadingPatterns(0x5a)		// Boss sprites
}


//# address-hook(0x06852c) end(0x068550)
function void fn06852c()
{
#if STANDALONE
	// Extend level chunks a bit
	u8[0xffff82ce] = u8[0xffff82cd]
	u8[0xffff832f] = u8[0xffff832e]
	u8[0xffff8390] = u8[0xffff838f]
#endif

	D5.u16 = 0x2f10
	if (camera.position.x.u16 < D5.u16)
	{
	#if STANDALONE
		// Set right border here already to prevent camera going too far for one frame
		move_area.right = D5.u16 + (getScreenWidth() - 320)
	#endif
		return
	}

#if STANDALONE
	// In Time Attack, skip most of the transition
	if (Game.isTimeAttack())
	{
		// Trigger fire wall immediately
		u16[0xffffeec6] = 0xff

		addPatternLoadingCue(0x083d48)		// Monitor, small explosion, and critter sprites

		UnloadObject()
		return
	}
#endif

	// Trigger AIZ 1 transition cutscene
	objA0.countdown_callback = 0x06857a

	// "spawnIdenticalChildObjects(0x06906a)" replaced by:
	spawnChildObjects(0x068dfa, 0, 0, 6)		// Passing miniboss drones in the background

	loadPaletteLine1(0x06917c)

	fn068556()
}


//# address-hook(0x068556) end(0x068572)
function void fn068556()
{
	objA0.base_state = 0x04
	objA0.countdown_value = 0xb4
	move_area.left = D5.u16
	move_area.right = D5.u16 + (getScreenWidth() - 320)
	playMusic(MUSIC_CTRL_FADEOUT)
}


//# address-hook(0x068572) end(0x068572)
function void fn068572()
{
}


//# address-hook(0x068574) end(0x068574)
function void fn068574()
{
	Object.CountdownAndTrigger()
}


//# address-hook(0x06857a) end(0x0685b6)
function void fn06857a()
{
	objA0.countdown_callback = 0x0685be
	objA0.base_state = 0x06

	// "spawnChildObjects(0x06905c)" replaced by:
	spawnChildObject(0x0686be, 0x00, 0, 32)		// Exhaust flames
	spawnChildObject(0x06870a, 0x02, -36, 8)	// Flamethrower nozzle

	// "spawnChildObjects(0x069072)" replaced by:
	spawnChildObject(0x06872c, 0x00, 0, -32)	// Rocket launcher
	spawnChildObject(0x06872c, 0x02, 9, -28)	// Rocket launcher
	spawnChildObject(0x06872c, 0x04, 18, -24)	// Rocket launcher

	fn06859c()
}


//# address-hook(0x06859c)
function void fn06859c()
{
	objA0.velocity.y = 0x100
	objA0.countdown_value = 0xaf
	playMusic(MUSIC_MINIBOSS_2)
	level.default_music.u8 = MUSIC_MINIBOSS_2
}


//# address-hook(0x0685b8) end(0x0685b8)
function void fn0685b8()
{
	fn0856b2()
}


//# address-hook(0x0685be) end(0x0685fa)
function void fn0685be()
{
	objA0.countdown_value = 0x7f
	objA0.countdown_callback = 0x06862e
	objA0.value39 = 3
	objA0.flags38 |= 0x02
	fn0685d8()
}


//# address-hook(0x0685d8)
function void fn0685d8()
{
	objA0.base_state = 0x08
	objA0.velocity.y = 0
	fn0685e2()
}


//# address-hook(0x0685e2)
function void fn0685e2()
{
	objA0.value3e = 0xc0
	objA0.velocity.y = 0xc0
	u16[A0 + 0x40] = 0x10
	objA0.flags38 &= ~0x01
}


//# address-hook(0x0685fc) end(0x068602)
function void fn0685fc()
{
	fn0847e6()
	fn0856b2()
}



//# address-hook(0x06862e) end(0x068640)
function void fn06862e()
{
	objA0.countdown_value = 0x10
	objA0.countdown_callback = 0x068646

	// "spawnChildObjects(0x069104)" replaced by:
	spawnChildObject(0x083f22, 0x00, 0, 0)		// Random explosions during the mid-stage fire transition
}


//# address-hook(0x068646) end(0x06868e)
function void fn068646()
{
	objA0.update_address = 0x068690
	loadPaletteLine1(0x06917c)

	u8[A0 + 0x28] = 0x0f
	playMusic(MUSIC_CTRL_FADEOUT)

	objA0.velocity.x = 0x400
	objA0.velocity.y = 0
	objA0.countdown_value = (global.zone_act == 0x0000) ? 0x120 : 0x40
	objA0.countdown_callback = addressof(Object.TriggerUnloading)

	// Trigger fire wall
	u8[0xffffeec6] = 0xff
}



// This object draws a part of the palmtree that should be rendered on top of the blimps in BG

//# address-hook(0x01e78a) end(0x01e7b0)
function void fn01e78a()
{
	objA0.mapping_offset = 0x21c3e8
	objA0.sprite_priority = 0x180
	objA0.box_size.x = 8
	objA0.render_flags = render_flag.WORLD
	objA0.sprite_attributes = (sprite_attribute.PALETTE.LINE2 | 0x0001)
	objA0.update_address = 0x01e7b0
	fn01e7b0()
}

//# address-hook(0x01e7b0)
function void fn01e7b0()
{
	DrawOrUnloadObject()
}
